/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mechanatrix.terrain;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.shadow.DirectionalLightShadowRenderer;
import com.jme3.util.SkyFactory;

/**
 *
 * @author dnigels
 */
public class TestGasStation extends SimpleApplication {

	private BulletAppState bulletAppState;

	public static void main(String[] args) {
		TestGasStation app = new TestGasStation();
		app.start();
	}

	private PhysicsSpace getPhysicsSpace() {
		return bulletAppState.getPhysicsSpace();
	}

	@Override
	public void simpleInitApp() {

		bulletAppState = new BulletAppState();
		stateManager.attach(bulletAppState);
		//bulletAppState.setDebugEnabled(true);

		this.flyCam.setMoveSpeed(100f);

		initTerrain();
		
		initLighting();
		//this.cam.lookAtDirection(new Vector3f(0.5f, 0f, 0.5f), Vector3f.UNIT_Y);
        this.getCamera().setLocation(new Vector3f(250f, 50f, 0f));
		this.getCamera().lookAt(new Vector3f(0f, 50f, 0f), Vector3f.UNIT_X.negate());
	}
	
	private void initLighting() {

		AmbientLight light = new AmbientLight();
		light.setColor(ColorRGBA.LightGray);
		rootNode.addLight(light);

		DirectionalLight sun = new DirectionalLight();
		sun.setDirection(new Vector3f(-FastMath.QUARTER_PI, -FastMath.QUARTER_PI, 1.0f));
		rootNode.addLight(sun);

		DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(assetManager, 1024, 3);
		dlsr.setLight(sun);
		viewPort.addProcessor(dlsr);
		rootNode.attachChild(SkyFactory.createSky(assetManager, "Textures/Hot Summer Day.dds", false));
		/*
		AmbientLight light = new AmbientLight();
		light.setColor(ColorRGBA.Gray);
		rootNode.addLight(light);

		DirectionalLight sun = new DirectionalLight();
		sun.setDirection(new Vector3f(-0.1f, -0.1f, 1.0f));
		rootNode.addLight(sun);

		DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(assetManager, 1024, 3);
		dlsr.setLight(sun);
		viewPort.addProcessor(dlsr);
		rootNode.attachChild(SkyFactory.createSky(assetManager, "Textures/Cool Cloud Sunset.dds", false));
		*/
	}

	private void initTerrain() {
		Box floorBox = new Box(512, 0.25f, 512);
		//Box floorBox = new Box(64, 0.25f, 64);
		floorBox.scaleTextureCoordinates(new Vector2f(64, 64));
		Geometry floorGeometry = new Geometry("Floor", floorBox);
		floorGeometry.setShadowMode(RenderQueue.ShadowMode.Receive);
		floorGeometry.setMaterial(assetManager.loadMaterial("MatDefs/ground_01.j3m"));
		floorGeometry.addControl(new RigidBodyControl(0));
		rootNode.attachChild(floorGeometry);
		getPhysicsSpace().add(floorGeometry);


		Spatial station = assetManager.loadModel("Models/architecture/station.j3o");
		station.setQueueBucket(RenderQueue.Bucket.Transparent);
		//station.rotate(0f, -FastMath.HALF_PI, 0f);
		station.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
		station.scale(FastMath.pow(2f, -6f)); //0.015625f
		rootNode.attachChild(station);
		
        station.addControl(new RigidBodyControl(0));
        bulletAppState.getPhysicsSpace().addAll(station);
		
		Spatial yucca = assetManager.loadModel("Models/flora/yucca.j3o");
		yucca.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
		yucca.scale(FastMath.pow(2f, -3f));
		//yucca.move(120f, 0.25f, 15f);
		//rootNode.attachChild(yucca);
		cloneAndPlace(yucca, 120f, 0.25f, 15f);
		cloneAndPlace(yucca, 160f, 0.25f, 150f);
		cloneAndPlace(yucca, 150f, 0.25f, 50f);
		cloneAndPlace(yucca, 180f, 0.25f, -30f);
		cloneAndPlace(yucca, 80f, 0.25f, 200f);
	}
	
	private void cloneAndPlace(Spatial spatial, float x, float y, float z) {
		Spatial clone = spatial.clone();
		clone.move(x, y, z);
		rootNode.attachChild(clone);
	}
}
